package com.debao.system.service.impl;

import java.util.List;

import com.debao.common.constant.WechatConstants;
import com.debao.common.wechat.WechatUtils;
import com.debao.system.mapper.SysUserMapper;
import com.debao.system.service.IConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.debao.system.mapper.CircleMapper;
import com.debao.system.domain.Circle;
import com.debao.system.service.ICircleService;

import javax.annotation.Resource;

/**
 * 帖子Service业务层处理
 * 
 * @author DeBao
 * @date 2024-07-01
 */
@Service
public class CircleServiceImpl implements ICircleService 
{
    @Autowired
    private CircleMapper circleMapper;

    @Resource
    private IConfigService configService;
    
    @Resource
    private SysUserMapper userMapper;



    /**
     * 查询帖子
     * 
     * @param id 帖子主键
     * @return 帖子
     */
    @Override
    public Circle selectCircleById(Long id)
    {
        return circleMapper.selectCircleById(id);
    }

    /**
     * 查询帖子列表
     * 
     * @param circle 帖子
     * @return 帖子
     */
    @Override
    public List<Circle> selectCircleList(Circle circle)
    {
        return circleMapper.selectCircleList(circle);
    }

    /**
     * 新增帖子
     * 
     * @param circle 帖子
     * @return 结果
     */
    @Override
    public int insertCircle(Circle circle)
    {
        String token = configService.selectValueByKey(WechatConstants.APP_TOKEN);
        String openId = userMapper.getOpenIdByUserId(circle.getUserId());
        WechatUtils.weiXinCheckContent(token, circle.getContent(),openId);
        return circleMapper.insertCircle(circle);
    }

    /**
     * 修改帖子
     * 
     * @param circle 帖子
     * @return 结果
     */
    @Override
    public int updateCircle(Circle circle)
    {
        String token = configService.selectValueByKey(WechatConstants.APP_TOKEN);
        String openId = userMapper.getOpenIdByUserId(circle.getUserId());
        WechatUtils.weiXinCheckContent(token, circle.getContent(),openId);
        return circleMapper.updateCircle(circle);
    }

    /**
     * 批量删除帖子
     * 
     * @param ids 需要删除的帖子主键
     * @return 结果
     */
    @Override
    public int deleteCircleByIds(Long[] ids)
    {
        return circleMapper.deleteCircleByIds(ids);
    }

    /**
     * 删除帖子信息
     * 
     * @param id 帖子主键
     * @return 结果
     */
    @Override
    public int deleteCircleById(Long id)
    {
        return circleMapper.deleteCircleById(id);
    }


    /**
     * 点赞
     * @param id
     */
    @Override
    public void addZan(Long id) {
        circleMapper.addZan(id);
    }
}
